//
//  QTEvaluateViewController.m
//  qdqts
//
//  Created by jonn on 14-7-1.
//  Copyright (c) 2014年 seas.cheng. All rights reserved.
//

#import "QTEvaluateViewController.h"
#import "QTClient.h"
#import "QTEvaTableViewCell.h"
#import "MJRefresh.h"

@interface QTEvaluateViewController ()

@end

@implementation QTEvaluateViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.navigationItem.title=[_name stringByAppendingString:@"评价"];
    if(_evaCell ==nil) {
        NSArray *nibArray = [[NSBundle mainBundle]loadNibNamed:@"QTEvaTableViewCell"owner:self options:nil];
        _evaCell = (QTEvaTableViewCell *)[nibArray objectAtIndex:0];
        
    }
    
    _tmpArray= [[NSMutableArray alloc]init];
    _tableView=[[UITableView alloc] initWithFrame:CGRectMake(0,0,SCREEN_WIDTH,SCREEN_HEIGHT) style:UITableViewStylePlain];
    _tableView.autoresizingMask = (UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight);
    _tableView.delegate = self;
    _tableView.dataSource = self;
    _tableView.separatorStyle=NO;
    _failureLabel.hidden=YES;
    [self.activity startAnimating];
    [self.view addSubview:_tableView];
    [self.view sendSubviewToBack:_tableView];
    [self setupRefresh];
    _num =@1;
    [self loadData];

    // Do any additional setup after loading the view from its nib.
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


#pragma mark - Table view data source


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
	//return [self.productEvaluation.evaluationInfoData count];
    return [_tmpArray count];
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    //在此使用样本Cell计算高度。
    NSString *content=((EvaluationInfoData *)[_tmpArray objectAtIndex:indexPath.row]).evaluationContent;
    [_evaCell setContentForLabel:content];
    NSLog(@"_evaCell:%@",_evaCell);
    NSLog(@"height:%f",[_evaCell contentHeight]);
    return [_evaCell contentHeight];
    
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"evaCell";
	QTEvaTableViewCell *cell = (QTEvaTableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if(cell ==nil) {
        NSArray *nibArray = [[NSBundle mainBundle]loadNibNamed:@"QTEvaTableViewCell"owner:self options:nil];
        cell = (QTEvaTableViewCell *)[nibArray objectAtIndex:0];
        
    }
    
	cell.selectionStyle = UITableViewCellStyleDefault;

    NSString *productSource=((EvaluationInfoData *)[_tmpArray objectAtIndex:indexPath.row]).productSource;
    cell.fromLabel.text=[[@"来自 “" stringByAppendingString:productSource==nil?@"未知":productSource] stringByAppendingString:@"”"];
    
    NSString *comment=((EvaluationInfoData *)[_tmpArray objectAtIndex:indexPath.row]).evaluationContent;
    cell.fromLabel.font=[UIFont systemFontOfSize:13];
    [cell setContentForLabel: comment];
    
	return cell;
}


#pragma mark - Table view delegate

/**
 *  集成刷新控件
 */
- (void)setupRefresh
{
    //上拉加载更多(进入刷新状态就会调用self的footerRereshing)
    [self.tableView addFooterWithTarget:self action:@selector(footerRereshing)];
}

#pragma mark 开始进入刷新状态

- (void)footerRereshing
{
    //加载下一组数据
    [self loadData];
}

-(void)loadData{
    NSLog(@"当前加载数据的起始值：%@",_num);
    [[QSClient instance] loadEvaluationInfo:_puid evaluationTypeId:_type startIndex:_num maxResults:@10 completionBlock:^(ProductEvaluation *productEvaluation, NSError *error){
        NSLog(@"++++++++++++++++Result MSG:%@",productEvaluation);
            NSLog(@"%@",_tmpArray);
            [self.activity stopAnimating];
            [_tmpArray addObjectsFromArray:productEvaluation.evaluationInfoData];
            NSLog(@"%@",_tmpArray);
            if (error) {
//                self.failureLabel.text=@"无法连接服务器，请检查网络";
//                [self.failureLabel setHidden:NO];
                UIAlertView *alert=[[UIAlertView alloc] initWithTitle:@"提示"
                                                              message:[NSString stringWithFormat:@"无法连接服务器，请稍后在试",nil]
                                                             delegate:self
                                                    cancelButtonTitle:@"确定"
                                                    otherButtonTitles:nil, nil];
                [alert show];
            }
            else if ([_tmpArray count]==0) {
                _failureLabel.hidden=NO;
            }else{
                _failureLabel.hidden=YES;
                _tableView.separatorStyle=YES;
                [self.tableView reloadData];
                _num=@(_num.intValue+10);
            }
        
            //结束所有的刷新状态
            [self.tableView headerEndRefreshing];
            [self.tableView footerEndRefreshing];
    }];
}

@end
